# coding=utf-8

import web
import json
from basecontroller import basecontroller
from services.monkeyservices import monkeyservices


class monkeycontroller(basecontroller):

    def POST(self, path):
        super(monkeycontroller, self).POST(path)
        data = web.data()
        # monkeyservices().execlinemonkey(json.loads(data))
        monkeyservices().execute_monkey_new(json.loads(data))
        return "1"

    def GET(self, path):
        print 'monkey path', path
        if path == 'getdevices':
            devices = monkeyservices().get_devices_has_state()
            return json.dumps(devices)
        elif 'getpackagesindevice' == path:
            devicesn = web.input().device
            pkginfo = monkeyservices().getpackageinfoindevice(devicesn)
            print pkginfo
            return json.dumps(pkginfo)
        try:
            import os
            print os.path.abspath(".")
            return open('./pages/helios/monkey/' + path).read()
        except IOError, e:
            print e.message
